package top.javai.www.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

public class TestSimpleQuartz {
    public static void main(String[] args) throws InterruptedException, SchedulerException {
        //1.创建调度器,会默认classpath下的quartz.properties
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        //2.创建Jobdetail,其中持有Job,并设置Job的name和group
        JobDetail job = JobBuilder.newJob(MyJob.class)
                .withIdentity("job1","group1")//name需要唯一不能重复
                .build();
        //3.创建触发器,设置开始,结束时间,重复
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1","group1")
                .withSchedule(SimpleScheduleBuilder.repeatSecondlyForever())//默认在开始时触发一次
                .endAt(DateBuilder.evenMinuteDate(new Date()))//下一分钟,到时间结束重复
                .build();
        //4.启动
        scheduler.scheduleJob(job,trigger);//一个触发器只能关联一个job
        scheduler.start();//启动调度器
        scheduler.shutdown(true);//如果有正在执行的job,等待其完成
    }
}
